package com.maoyue.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.maoyue.demo.dao.UserDAO;
import com.maoyue.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("Zouxiang")
@Slf4j
public class ZouXiangController {

    @Resource
    private UserDAO userDao;

    @GetMapping("/read")
    private String getUsers(){

        EasyExcel.read("E:\\interview\\easydemo\\src\\main\\resources\\static\\test.xlsx", User.class, new PageReadListener<User>(dataList -> {
            for (User demoData : dataList) {
                log.info("读取到一条数据{}", demoData);
                userDao.insert(demoData);
            }
        })).sheet().doRead();
        return "完成";
    }

    @GetMapping("/write")
    private String writeUsers(){

        String filePath = "E:\\interview\\easydemo\\src\\main\\resources\\static\\new.xlsx";

        EasyExcel.write(filePath, User.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return userDao.selectList(null);

                });
        return "成功";
    }





}
